//
//  AccountModel.m
//  GuiderHealth
//
//  Created by zyming on 2020/9/1.
//  Copyright © 2020 YM&EF. All rights reserved.
//

#import "AccountModel.h"
#import "BaseToolsKit.h"
#import "JQFMDB.h"

/** 账户信息 */
#define Key_AccountInfoPath [[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingString:@"AccountInfo.data"]

/** 当前选择的账户信息 */
#define Key_SelectedAccountInfoPath [[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingString:@"SelectedAccountInfo.data"]


#define GDDB [JQFMDB shareDatabase:@"Guider.sqlite"]
#define GDDBTable_Account @"AccountModel"
#define GDDBTable_SelectedAccount @"SelectedAccountModel"
@implementation AccountModel

//当有模型有新字段时调用
+ (void)AlterTableAccount{
    //是否存在表
    if ([GDDB jq_isExistTable:GDDBTable_Account]) {
        [GDDB jq_alterTable:GDDBTable_Account dicOrModel:[AccountModel class]];
    }
    if ([GDDB jq_isExistTable:GDDBTable_SelectedAccount]) {
        [GDDB jq_alterTable:GDDBTable_SelectedAccount dicOrModel:[AccountModel class]];
    }
}

+ (void)saveAccountInfo:(AccountModel *)account{
    
    //是否存在表,不存在则创建
    if (![GDDB jq_isExistTable:GDDBTable_Account]) {
        BOOL createSuccess = [GDDB jq_createTable:GDDBTable_Account dicOrModel:[AccountModel class]];
        NSLog(@"%@表创建会否成功:%d",GDDBTable_Account,createSuccess);
    }
    
    //查询当前表是否存在改用户
    NSArray <AccountModel *>*array = [GDDB jq_lookupTable:GDDBTable_Account dicOrModel:[AccountModel class] whereFormat:[NSString stringWithFormat:@"where accountId = '%@'",account.accountId]];
    
    //存在则更新,不存在则新增
    if (array.count > 0) {
        [GDDB jq_inDatabase:^{
            BOOL isSuccess = [GDDB jq_updateTable:GDDBTable_Account dicOrModel:account whereFormat:[NSString stringWithFormat:@"where accountId = '%@'",account.accountId]];
            NSLog(@"用户AccountId:%@ 是否刷新成功:%d",account.accountId,isSuccess);
            [GDDB close];
        }];
    }else{
        [GDDB jq_inDatabase:^{
            BOOL isSuccess = [GDDB jq_insertTable:GDDBTable_Account dicOrModel:account];
            NSLog(@"用户AccountId:%@ 是否存储成功:%d",account.accountId,isSuccess);
            [GDDB close];
        }];
    }
}
+ (AccountModel *)getAccountInfo{
    //查询当前表是否存在改用户
    __block AccountModel *account = [AccountModel new];
    [GDDB jq_inDatabase:^{
        NSArray <AccountModel *>*array = [GDDB jq_lookupTable:GDDBTable_Account dicOrModel:[AccountModel class] whereFormat:nil];
        [GDDB close];
        if (array.count > 0) {
            account = array.lastObject;
        }
    }];
    return account;
}
+ (void)saveSelectAccountInfo:(AccountModel *)account{
    
    //是否存在表,不存在则创建
    if (![GDDB jq_isExistTable:GDDBTable_SelectedAccount]) {
        BOOL createSuccess = [GDDB jq_createTable:GDDBTable_SelectedAccount dicOrModel:[AccountModel class]];
        NSLog(@"%@表创建会否成功:%d",GDDBTable_SelectedAccount,createSuccess);
    }
    
    //查询当前表是否存在改用户
    NSArray <AccountModel *>*array = [GDDB jq_lookupTable:GDDBTable_SelectedAccount dicOrModel:[AccountModel class] whereFormat:[NSString stringWithFormat:@"where accountId = '%@'",account.accountId]];
    
    //存在则更新,不存在则新增
    if (array.count > 0) {
        [GDDB jq_inDatabase:^{
            BOOL isSuccess = [GDDB jq_updateTable:GDDBTable_SelectedAccount dicOrModel:account whereFormat:[NSString stringWithFormat:@"where accountId = '%@'",account.accountId]];
//            BOOL isSuccess = [GDDB jq_updateTable:GDDBTable_SelectedAccount dicOrModel:account whereFormat:nil];
            NSLog(@"用户AccountId:%@ 是否刷新成功:%d",account.accountId,isSuccess);
            [GDDB close];
        }];
    }else{
        [GDDB jq_inDatabase:^{
             [GDDB jq_deleteAllDataFromTable:GDDBTable_SelectedAccount];
        }];
        [GDDB jq_inDatabase:^{
            BOOL isSuccess = [GDDB jq_insertTable:GDDBTable_SelectedAccount dicOrModel:account];
            NSLog(@"用户AccountId:%@ 是否存储成功:%d",account.accountId,isSuccess);
            [GDDB close];
        }];
    }
}
+ (AccountModel *)getSelectAccountInfo{
    //查询当前表是否存在改用户
    __block AccountModel *account = [AccountModel new];
    [GDDB jq_inDatabase:^{
        NSArray <AccountModel *>*array = [GDDB jq_lookupTable:GDDBTable_SelectedAccount dicOrModel:[AccountModel class] whereFormat:nil];
        [GDDB close];
        if (array.count > 0) {
            account = array.lastObject;
        }
    }];
    if (!account.accountId || account.accountId.length < 1) {
        account = [AccountModel getAccountInfo];
    }
    return account;
}

/** 清空用户信息 */
+ (void)clearAccountInfo {
    
    setMyDeviceCode(@"");
    setMyUserGroup(@"");
    setAppleUserID(nil);
    
    [GDDB jq_inDatabase:^{
        [GDDB jq_deleteAllDataFromTable:GDDBTable_Account];
    }];
    [GDDB jq_inDatabase:^{
        [GDDB jq_deleteAllDataFromTable:GDDBTable_SelectedAccount];
    }];
}

+ (NSString *)GetBaseURL{
    NSString *baseURL = @"http://api.guidertech.com";
    if ([UserDefault objectForKey:@"key_BaseURL"]) {
        baseURL = [UserDefault objectForKey:@"key_BaseURL"];
    }
    return baseURL;
}
+ (void )SetBaseURL:(NSString *)baseURL{
    [UserDefault setObject:baseURL forKey:@"key_BaseURL"];
    [UserDefault synchronize];
}

+ (NSDictionary *)replacedKeyFromPropertyName {
    return @{
        @"user_id" : @"id",
    };
}
+(NSDictionary *)mj_objectClassInArray{
      return @{@"deviceList" : @"DeviceModel"};
  }

- (NSString *)deviceCode{
    return ToString(_deviceCode);
}
- (NSString *)pointShowType{
    if (!_pointShowType) {
        return @"LP";
    }
    return _pointShowType;
}

- (NSString *)im_accountId{
    NSString *imAccountid = [NSString stringWithFormat:@"USER%@",_accountId];
    return imAccountid;
}

- (NSString *)relationShip {
    if (!_relationShip || _relationShip.length == 0) {
        return _name;
    }
    return _relationShip;
}


+ (NSString *)getSelectedAccountId {
    AccountModel *acc = [AccountModel getSelectAccountInfo];
    if (!acc.accountId || acc.accountId.length < 1) {
        return [AccountModel getAccountInfo].accountId;
    }
    return [NSString stringWithFormat:@"%@",acc.accountId];
}


+ (NSString *)getTargetSteps{
    NSString *target_step = [[NSUserDefaults standardUserDefaults] objectForKey:@"GD_targetStep"];
    NSString *targetStep = @"10000";
    if (target_step && target_step.length > 0) {
        targetStep = target_step ;
    }
    return targetStep;
}
//+ (void)setTargetSteps:(NSString *)step{
//    [[NSUserDefaults standardUserDefaults] setValue:step forKey:@"GD_targetStep"];
//    [[NSUserDefaults standardUserDefaults] synchronize];
//}

-(NSString *)descDetail{
    if (!_descDetail) {
        return @"";
    }
    return _descDetail;
}
- (NSString *)accountId {
    if (!_accountId) {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",_accountId];
}
- (NSString *)weight {
    if (!_weight || _weight == nil || _weight == NULL || [_weight isEqual:[NSNull null]]) {
        return @"0";
    }
    return [NSString stringWithFormat:@"%d",_weight.intValue];
}
-(NSString *)height {
    if (!_height) {
        return @"0";
    }
    return [NSString stringWithFormat:@"%@",_height];
}
- (NSString *)headUrl {
    if (!_headUrl) {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",_headUrl];
}
- (NSString *)name {
    if (!_name) {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",_name];
}
- (NSString *)gender{
    if (!_gender) {
        return @"MAN";
    }
    return [NSString stringWithFormat:@"%@",_gender];
}
- (NSString *)birthday{
    if (!_birthday) {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",_birthday];
}

- (NSString *)rssi{
    if (!_rssi) {
        _rssi = [NSString stringWithFormat:@"%@",_rssi];
    }
    return _rssi;
}
- (NSString *)deviceMode{
    if (!_deviceMode) {
        _deviceMode = [NSString stringWithFormat:@"%@",_deviceMode];
    }
    return _deviceMode;
}
- (NSString *)targetSteps{
    if (!_targetSteps) {
        _targetSteps = @"10000";
    }
    return _targetSteps;
}

///设置deviceType
+ (void)setMainDeviceType:(NSString *)deviceType{
    [UserDefault setObject:deviceType forKey:@"kBase_DeviceType"];
    [UserDefault synchronize];
}
///获取deviceType
+ (NSString *)getMainDeviceType{
    NSString *deviceType = @"ALL";
    if ([UserDefault objectForKey:@"kBase_DeviceType"]) {
        deviceType = [UserDefault objectForKey:@"kBase_DeviceType"];
    }
    return deviceType;
}

+ (UIColor *)GetMainColor{
    if ([AccountModel GetMainTypeOrange]) {
        return RGB(242, 138, 56);
    }else{
        return RGB(67, 186, 254);
    }
}
+ (BOOL)GetMainTypeOrange{
    NSString *deviceType = [AccountModel getMainDeviceType];
    if (
//        [deviceType isEqualToString:@"ALL"]||
        [deviceType isEqualToString:@"GUIDERHEALTH"]
        || [deviceType isEqualToString:@"G700"]
        ) {
        return NO;
    }else{
        return YES;
    }
}

@end


#define GDDBTable_Device @"DeviceModel"
@implementation DeviceModel

//+ (void)saveDeviceCode:(NSString *)deviceCode{
//    DeviceModel *deviceModel = [DeviceModel getDeviceInfo];
//    deviceModel.deviceCode
//}
//+ (NSString *)getDeviceCode{
//    NSString *deviceCode = @"";
//    if ([DeviceModel getDeviceInfo] && [DeviceModel getDeviceInfo].deviceCode.length > 0) {
//        deviceCode =[DeviceModel getDeviceInfo].deviceCode;
//    }
//    return deviceCode;
//}

+ (void)saveDeviceInfo:(DeviceModel *)device{
    
    //是否存在表,不存在则创建
    if (![GDDB jq_isExistTable:GDDBTable_Device]) {
        BOOL createSuccess = [GDDB jq_createTable:GDDBTable_Device dicOrModel:[DeviceModel class]];
        NSLog(@"%@表创建会否成功:%d",GDDBTable_Device,createSuccess);
    }
    
    NSArray <DeviceModel *>*array = [GDDB jq_lookupTable:GDDBTable_Device dicOrModel:[DeviceModel class] whereFormat:nil];
    
    //存在则更新,不存在则新增
    if (array.count > 0) {
        [GDDB jq_inDatabase:^{
            BOOL isSuccess = [GDDB jq_updateTable:GDDBTable_Device dicOrModel:device whereFormat:nil];
            NSLog(@"设备deviceCode:%@ 是否刷新成功:%d",device.deviceCode,isSuccess);
            [GDDB close];
        }];
    }else{
        [GDDB jq_inDatabase:^{
            BOOL isSuccess = [GDDB jq_insertTable:GDDBTable_Device dicOrModel:device];
            NSLog(@"设备deviceCode:%@ 是否存储成功:%d",device.deviceCode,isSuccess);
            [GDDB close];
        }];
    }
}
+ (DeviceModel *)getDeviceInfo{
    //查询当前表是否存在改用户
    __block DeviceModel *device = [DeviceModel new];
    [GDDB jq_inDatabase:^{
        NSArray <DeviceModel *>*array = [GDDB jq_lookupTable:GDDBTable_Device dicOrModel:[DeviceModel class] whereFormat:nil];
        [GDDB close];
        if (array.count > 0) {
            device = array.lastObject;
        }
        for (DeviceModel *mode in array) {
            NSLog(@"lllllllllllll:%@",mode.deviceCode);
        }
    }];
    return device;
}

+ (NSString *)getSelectDeviceCode{
    DeviceModel *deviceModel = [DeviceModel getDeviceInfo];
    if (deviceModel.deviceCode && deviceModel.deviceCode.length > 0) {
        return deviceModel.deviceCode;
    }else if ([AccountModel getSelectAccountInfo].deviceList.count > 0) {
        deviceModel = [AccountModel getSelectAccountInfo].deviceList.firstObject;
        return deviceModel.deviceCode;
    }
    return @"";
}
+ (NSString *)getSelectDeviceType{
    DeviceModel *deviceModel = [DeviceModel getDeviceInfo];
    if (deviceModel.deviceType && deviceModel.deviceType.length > 0) {
        return deviceModel.deviceType;
    }else if ([AccountModel getSelectAccountInfo].deviceList.count > 0) {
        deviceModel = [AccountModel getSelectAccountInfo].deviceList.firstObject;
        return deviceModel.deviceType;
    }
    return @"ALL";
}
@end
